package com.cmpe.av;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cmpe.av.logic.ReportLogic;
import com.cmpe.av.model.Report;

import sun.reflect.ReflectionFactory.GetReflectionFactoryAction;

/**
 * Servlet implementation class Login
 */
@WebServlet("/HomePages")
public class HomePages extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HomePages() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		ReportLogic msAccess;
		List<Report> reports= new ArrayList<Report>();
		HttpSession session = request.getSession();
				
		if(request.getParameter("button").equals("Add Report")){
			request.getRequestDispatcher("addReport.jsp").forward(request, response);
		
		}
		else if(request.getParameter("button").equals("Register")){
			response.sendRedirect("default.jsp");
		}
		else if(request.getParameter("button").equals("Log Out")){
			session = request.getSession(false); 
			if(session!=null){ 
				session.invalidate(); 
			} 
			response.sendRedirect("./default.jsp");
		}
		else if(request.getParameter("button").equals("View my subscriptions")){
			try {
				msAccess = new ReportLogic();
				int userID = (Integer) session.getAttribute("userID");
				reports = msAccess.searchSubscriptions(userID);
				if(reports.size()!=0)
                    request.setAttribute("subscriptions", true);
				request.setAttribute("subsReports", reports);
				request.getRequestDispatcher("homeMember.jsp").forward(
						request, response);
				
			} catch (Exception e) {
				e.printStackTrace();
			} 	
		}
		else if(request.getParameter("button").equals("View my reports")){
			try {
				msAccess = new ReportLogic();
				int userID = (Integer) session.getAttribute("userID");
				reports = msAccess.viewOwnReports(userID);
				if(reports.size()!=0)
                    request.setAttribute("reportFlag", true);
				
				request.setAttribute("ownReports", reports);
				request.getRequestDispatcher("homeMember.jsp").forward(
						request, response);
				
			} catch (Exception e) {
				e.printStackTrace();
			} 	
		}
		else{
			response.sendRedirect("searchPage.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
